/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HAL_Led.c
* version:         1.0
* created_by:      Continental
* date_created:    June 7 2013
*=============================================================================*/
/* DESCRIPTION : Source file for abstraction layer for LED                    */
/*============================================================================*/
/* FUNCTION COMMENT : Source file abstraction layer for LED                   */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 07/jun/2013 |                               | Continental      */
/* Initial version for the Lab 2 using Freescale Bolero micro.                */
/*----------------------------------------------------------------------------*/
/*  2.0      | 22/jun/2013 |                               | Eunice Ibarra    */
/* Modified to use naming conventions						                  */
/*============================================================================*/

#include "sysinit.h"
#include "HAL_Led.h"

#include "MAL_IOapi.h"
#include "MAL_Timer.h"


/* 0 to 9 because they are 10 LEDs */
static T_SBYTE rsb_LastLedOn = 9;  


/****************************************************************************************************/
/**
* \brief    Configures individual GPIO pins to either input or output functionality.  
* \author   Abraham Tezmol
* \param    T_UBYTE channel - GPIO channel to be configured
* \param    T_UBYTE input_output - selection of input/output functionality (has impact on output impedance selection)
* \param    T_UBYTE Open_drain - Push pull or open drain selection modes 
* \return   void
*/
void HAL_Led_InitChannel(T_UBYTE lub_Channel, T_UBYTE lub_InputOutput, T_UBYTE lub_OpenDrain)
{
	IO_SetPinFctn(lub_Channel,PA_GPIO);                 /* GPIO */
	
    if (lub_InputOutput == GPIO_OUTPUT)
    {    	
    	IO_SetPinAsOutput(lub_Channel);                 /* Output buffer enable */
    	IO_PinOpenDrainEnable(lub_Channel, lub_OpenDrain); /* Open drain option enable */	
    	
    }
    else if (lub_InputOutput == GPIO_INPUT)
    {
    	IO_SetPinAsInput(lub_Channel);					/* Input buffer enable */	
    }

}

/****************************************************************************************************/
/**
* \brief    Initialize GPIO port connected to LEDs on eval board
* \author   Abraham Tezmol
* \param    void 
* \return   void
*/
void HAL_Led_Init(void)
{
    /* Data Port E initialization */
	HAL_Led_InitChannel(LED_WINDOW1,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW1, LED_ON); 
	HAL_Led_InitChannel(LED_WINDOW2,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW2, LED_ON);
	HAL_Led_InitChannel(LED_WINDOW3,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW3, LED_ON);
	HAL_Led_InitChannel(LED_WINDOW4,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW4, LED_ON);
	HAL_Led_InitChannel(LED_WINDOW5,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW5, LED_ON);
	HAL_Led_InitChannel(LED_WINDOW6,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW6, LED_ON);
	HAL_Led_InitChannel(LED_WINDOW7,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW7, LED_ON);
	HAL_Led_InitChannel(LED_WINDOW8,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW8, LED_ON);
	HAL_Led_InitChannel(LED_WINDOW9,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW9, LED_ON);
	HAL_Led_InitChannel(LED_WINDOW10,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW10, LED_ON); 
	
	
	HAL_Led_InitChannel(LED_WINDOW_UP,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW_UP, LED_OFF);
	HAL_Led_InitChannel(LED_WINDOW_DOWN,GPIO_OUTPUT,GPIO_OPEN_DRAIN_ENABLE);  
	IO_PinWrite(LED_WINDOW_DOWN, LED_OFF);
	
	MAL_Timer_Init();
}

/****************************************************************************************************/

void HAL_Led_TurnOn(T_UBYTE lub_Led)
{
	IO_PinWrite(lub_Led, LED_ON);
}

void HAL_Led_TurnOff(T_UBYTE lub_Led)
{
	IO_PinWrite(lub_Led, LED_OFF);
}

void HAL_Led_IsWindowClose(PTR_UBYTE lpub_Close)
{
	if(rsb_LastLedOn == LED_CLOSED_WINDOW)
	{
		*lpub_Close = TRUE;
	}
	else
	{
		*lpub_Close = FALSE;
	}
}

void HAL_Led_IsWindowOpen(PTR_UBYTE lpub_Open)
{
	if(rsb_LastLedOn == LED_OPENED_WINDOW)
	{
		*lpub_Open = TRUE;
	}
	else
	{
		*lpub_Open = FALSE;
	}
}

void HAL_Led_WindowStill(void)
{
	HAL_Led_TurnOff(LED_WINDOW_UP);
	HAL_Led_TurnOff(LED_WINDOW_DOWN);
	
}

void HAL_Led_CloseNext(void)
{
	if(rsb_LastLedOn != LED_CLOSED_WINDOW)
	{
		HAL_Led_TurnOn(LED_WINDOW_UP);
		HAL_Led_TurnOff(LED_WINDOW_DOWN);
		
		rsb_LastLedOn++;
		HAL_Led_TurnOn((LED_WINDOW1 + (T_UBYTE)rsb_LastLedOn));
	}
}

void HAL_Led_OpenNext(void)
{
	if(rsb_LastLedOn != LED_OPENED_WINDOW)
	{
		HAL_Led_TurnOn(LED_WINDOW_DOWN);
		HAL_Led_TurnOff(LED_WINDOW_UP);
		
		HAL_Led_TurnOff((LED_WINDOW1 + (T_UBYTE)rsb_LastLedOn));
		rsb_LastLedOn--;
	}
}

void HAL_Led_Init400ms(void)
{
	MAL_Timer400ms_Init_channel();
}

void HAL_Led_HasCounted400ms(PTR_UBYTE lpub_Count400ms)
{
	T_ULONG lub_TimerFlag;
	
	lub_TimerFlag = MAL_Read400msFLAG();
	
	if(lub_TimerFlag == TRUE)
	{
		*lpub_Count400ms = TRUE;	
	}
	else
	{
		*lpub_Count400ms = FALSE;
	}	
	
}

void HAL_Timer_HasCounted5s(PTR_UBYTE lpub_Timer5s)
{
	
	T_ULONG lub_TimerFlag;
	
	lub_TimerFlag = MAL_Read5sFLAG();
	
	if(lub_TimerFlag == TRUE)
	{
		*lpub_Timer5s = TRUE;	
	}
	else
	{
		*lpub_Timer5s = FALSE;
	}
}


void HAL_Timer_Init5s(void)
{
	MAL_Timer5s_Init_channel();
}




